package com.ccjq.shosestore.member.service.impl;

import com.ccjq.shosestore.core.utils.Md5Utils;
import com.ccjq.shosestore.core.utils.PojoUtils;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.dao.impl.UserDaoImpl;
import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.po.UserModel;
import com.ccjq.shosestore.member.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao dao;

    public UserServiceImpl() {
        dao = new UserDaoImpl();
    }

    public boolean existx(String username) {
        UserModel model = dao.findUserByName(username);
        if (model == null) {
            return false;
        } else {
            return true;
        }

    }

    public String saveUser(User user) {
        String userId = UuidUtils.generateShortUUID("U", 10);
        user.setUserid(userId);
        user.setPassword(Md5Utils.encoderByMd5(user.getPassword()));
        int result = dao.save(PojoUtils.bo2po(user, UserModel.class));
        return userId;
    }

    public User findUserById(String userId) {
        UserModel model = dao.findUserById(userId);
        User user = PojoUtils.po2bo(model, User.class);
        return user;
    }

    public User findUserByNamePw(String username, String password) {
        password = Md5Utils.encoderByMd5(password);
        UserModel model = dao.findUserByNamePw(username, password);
        User user = PojoUtils.po2bo(model, User.class);
        return user;
    }
}
